<template>
  <el-dialog
    :title="!dataForm.schoolId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="学校名称" prop="schoolName">
      <el-input v-model="dataForm.schoolName" placeholder="学校名称"></el-input>
    </el-form-item>
    <el-form-item label="省份" prop="provinceName">
      <el-input v-model="dataForm.provinceName" placeholder="省份"></el-input>
    </el-form-item>
    <el-form-item label="城市" prop="cityName">
      <el-input v-model="dataForm.cityName" placeholder="城市"></el-input>
    </el-form-item>
    <el-form-item label="区县" prop="districtName">
      <el-input v-model="dataForm.districtName" placeholder="区县"></el-input>
    </el-form-item>
    <el-form-item label="学校地址" prop="schoolAddress">
      <el-input v-model="dataForm.schoolAddress" placeholder="学校地址"></el-input>
    </el-form-item>
    <el-form-item label="学校性质" prop="schoolKind">
      <el-input v-model="dataForm.schoolKind" placeholder="学校性质"></el-input>
    </el-form-item>
    <el-form-item label="坐标位置" prop="schoolCoordinates">
      <el-input v-model="dataForm.schoolCoordinates" placeholder="坐标位置"></el-input>
    </el-form-item>
    <el-form-item label="类型(0.幼儿园 1.小学 2.初中 3.高中)" prop="schoolType">
      <el-input v-model="dataForm.schoolType" placeholder="类型(0.幼儿园 1.小学 2.初中 3.高中)"></el-input>
    </el-form-item>
    <el-form-item label="缩略图" prop="schoolImg">
      <el-input v-model="dataForm.schoolImg" placeholder="缩略图"></el-input>
    </el-form-item>
    <el-form-item label="介绍" prop="schoolIntroduce">
      <el-input v-model="dataForm.schoolIntroduce" placeholder="介绍"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          schoolId: 0,
          schoolName: '',
          provinceName: '',
          cityName: '',
          districtName: '',
          schoolAddress: '',
          schoolKind: '',
          schoolCoordinates: '',
          schoolType: '',
          schoolImg: '',
          schoolIntroduce: ''
        },
        dataRule: {
          schoolName: [
            { required: true, message: '学校名称不能为空', trigger: 'blur' }
          ],
          provinceName: [
            { required: true, message: '省份不能为空', trigger: 'blur' }
          ],
          cityName: [
            { required: true, message: '城市不能为空', trigger: 'blur' }
          ],
          districtName: [
            { required: true, message: '区县不能为空', trigger: 'blur' }
          ],
          schoolAddress: [
            { required: true, message: '学校地址不能为空', trigger: 'blur' }
          ],
          schoolKind: [
            { required: true, message: '学校性质不能为空', trigger: 'blur' }
          ],
          schoolCoordinates: [
            { required: true, message: '坐标位置不能为空', trigger: 'blur' }
          ],
          schoolType: [
            { required: true, message: '类型(0.幼儿园 1.小学 2.初中 3.高中)不能为空', trigger: 'blur' }
          ],
          schoolImg: [
            { required: true, message: '缩略图不能为空', trigger: 'blur' }
          ],
          schoolIntroduce: [
            { required: true, message: '介绍不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.schoolId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.schoolId) {
            this.$http({
              url: this.$http.adornUrl(`/backstage/school/info/${this.dataForm.schoolId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.schoolName = data.school.schoolName
                this.dataForm.provinceName = data.school.provinceName
                this.dataForm.cityName = data.school.cityName
                this.dataForm.districtName = data.school.districtName
                this.dataForm.schoolAddress = data.school.schoolAddress
                this.dataForm.schoolKind = data.school.schoolKind
                this.dataForm.schoolCoordinates = data.school.schoolCoordinates
                this.dataForm.schoolType = data.school.schoolType
                this.dataForm.schoolImg = data.school.schoolImg
                this.dataForm.schoolIntroduce = data.school.schoolIntroduce
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/backstage/school/${!this.dataForm.schoolId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'schoolId': this.dataForm.schoolId || undefined,
                'schoolName': this.dataForm.schoolName,
                'provinceName': this.dataForm.provinceName,
                'cityName': this.dataForm.cityName,
                'districtName': this.dataForm.districtName,
                'schoolAddress': this.dataForm.schoolAddress,
                'schoolKind': this.dataForm.schoolKind,
                'schoolCoordinates': this.dataForm.schoolCoordinates,
                'schoolType': this.dataForm.schoolType,
                'schoolImg': this.dataForm.schoolImg,
                'schoolIntroduce': this.dataForm.schoolIntroduce
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
